package com.sbj.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbj.dto.req.CategoryReq;
import com.sbj.dto.res.CategoryRes;
import com.sbj.entity.Category;
import com.sbj.mapper.CategoryMapper;
import com.sbj.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Wrapper;

/**
 * @author summer
 * @version 1.0
 * @date 2024/8/9 12:01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public Page<CategoryRes> lists(CategoryReq categoryReq) {
        Page<Category> page=new Page<>(categoryReq.getPageNum(),categoryReq.getPageSize());
        LambdaQueryWrapper<Category> eq = Wrappers.lambdaQuery(Category.class).like(StrUtil.isNotEmpty(categoryReq.getName()), Category::getName, categoryReq.getName());
        return Convert.convert(new TypeReference<Page<CategoryRes>>() {},this.page(page,eq));
    }
}
